<?php

namespace Biz\model\activity;

use Doraemon\model\Guide;
use \Doraemon\model\Blank;
use Doraemon\service\Content\AdmContent;
use Doraemon\tools\BlockType;
use Doraemon\tools\IdParse;
use Doraemon\tools\ImgUrl;
use Doraemon\model\Article as mArticle;

class PageModuleH5Obj extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'page_module_h5_obj';



    public static function getModuleArticles($pageId, $moduleId)
    {
        $list = self::select('*', [
            'page_id' => $pageId,
            'module_id' => $moduleId,
            'article_status' => self::STATUS_NORMAL,
        ]);
        return empty($list) ? [] : $list;
    }

    public static function getModuleArticle($pageId, $moduleId)
    {
        return self::select('aid', [
            'page_id' => $pageId,
            'module_id' => $moduleId,
            'article_status' => self::STATUS_NORMAL,
        ]);
    }

    public static function addModuleH5ObjData($data = [], $id = 0)
    {
        if ($id > 0) {
            $data['edittime'] = date('Y-m-d H:i:s');
            self::update($data, ['id' => $id]);
        } else {
            $data['addtime'] = date('Y-m-d H:i:s');
            $data['obj_status'] = self::STATUS_NORMAL;
            self::insert($data);
        }
    }


    public static function saveModuleSort($sortInfo)
    {
        foreach ($sortInfo as $id => $sort) {
            self::update(['sort' => $sort], ['id' => $id]);
        }
    }


    public static function getArticleByModuleId($moduleId, $format = true)
    {
        $list = self::select("*",
            [
                'AND' => [
                    'module_id' => $moduleId,
                    'obj_status' => self::STATUS_NORMAL
                ],
                'ORDER' => [
                    'sort',
                    'id'
                ]
            ]);
        if ($list && $format) {
            //内容id分组
            $contentMap = [];
            foreach ($list as $key => $item) {
                $objId = $item['obj_id'];
                $objType = IdParse::getType($objId);
                $contentMap[$objType][] = $objId;
            }
            foreach ($contentMap as $objType => $aids) {
                $contentMap[$objType] = AdmContent::get($objType, $aids);
            }

            foreach ($list as $key => $item) {
                $objId = $item['obj_id'];
                $objType = IdParse::getType($objId);
                $list[$key]['list_info'] = $contentMap[$objType][$objId] ?? [];
                $list[$key]['obj_tag'] = explode(',', $item['obj_tag']);
                $list[$key]['pic_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $item['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
//                $list[$key]['article_type_desc'] = self::ADMIN_ARTICLE_TYPE_MAP[$item['article_type']] ?? '';
//                switch ($item['article_type']) {
//                    case BlockType::TYPE_ARTICLE:
//                        $list[$key]['house_info'] = [
//                            'title' => isset($articles[$aid]) ? $articles[$aid]['title'] : '',
//                            'cover_pic' => isset($articles[$aid]) && $articles[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $articles[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
//                        ];
//                        break;
//                    case BlockType::TYPE_PHOTO:
//                        $list[$key]['house_info'] = [
//                            'title' => isset($guides[$aid]) ? $guides[$aid]['title'] : '',
//                            'cover_pic' => isset($guides[$aid]) && $guides[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $guides[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
//                        ];
//                        break;
//                }
            }
        }
        return $list;
    }

    public static function changeStatusById($id, $status)
    {
        if (self::update(['obj_status' => $status], ["id" => $id])->rowcount()) {
            return $id;
        }
    }
}